package com.android.place360.google_places;

import com.android.place360.services.HttpService;
import com.google.gson.Gson;

public class PlaceRequest {

	public static String REQUEST_URL =  "https://maps.googleapis.com/maps/api/place/search/json?";
	public static String REQUEST_ERROR =  "Lack of required parameters";
	
	private String location;
	private double radius;
	private String types;
	private String language;
	private String name;
	private boolean sensor;
	private String key;
	public PlaceRequest() {
		super();
	}
	public PlaceRequest(String location, double radius, String types,
			String language, String name, boolean sensor, String key) {
		super();
		this.location = location;
		this.radius = radius;
		this.types = types;
		this.language = language;
		this.name = name;
		this.sensor = sensor;
		this.key = key;
	}
	
	public String getRequestString(){
		String request = REQUEST_URL;
		if(!parameterValid(location)) 	return REQUEST_ERROR;
		if(!parameterValid(radius)) 	return REQUEST_ERROR;
		if(!parameterValid(sensor)) 	return REQUEST_ERROR;
		if(!parameterValid(key))		return REQUEST_ERROR;
		
		request += "location=" 	+ location;
		request += "&radius="	+ radius;
		request += "&sensor="	+ sensor;
		request += "&key="		+ key;
		
		if(parameterValid(types)) 		request += "&types=" + types;
		if(parameterValid(language)) 	request += "&language=" + language;
		if(parameterValid(name)) 		request += "&name=" + name;
		
		request = request.replace(" ", "%20").trim();
		
		return request;
	}
	
	public String sendRequest(){
		
		String request = getRequestString();
		if(request.equals(REQUEST_ERROR)) return REQUEST_ERROR;
		return HttpService.SendRequest(getRequestString());
		
	}
	
	public PlaceResponse getResponse(){
		try {
			String json = sendRequest();
			if(json.equals(REQUEST_ERROR)) return null;
			
			Gson gson = new Gson();
			return gson.fromJson(json, PlaceResponse.class);
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private boolean parameterValid(String para){
		if(para==null || para.trim().equals("")) return false;
		return true;
	}
	private boolean parameterValid(Object para){
		if(para==null) return false;
		return true;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}
	public String getTypes() {
		return types;
	}
	public void setTypes(String types) {
		this.types = types;
	}
	public String getLanguage() {
		return language;
	}
	public void setLanguage(String language) {
		this.language = language;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public boolean isSensor() {
		return sensor;
	}
	public void setSensor(boolean sensor) {
		this.sensor = sensor;
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	
	
	
}
